//
//  YHMyMerchantManageSubViewController.m
//  YHApp
//
//  Created by -- on 2021/12/19.
//

#import "HAMyMerchantManageSubVC.h"
#import "HACanalItemMo.h"
#import "HAMerchantManageMo.h"
#import "HAAllMerchantVC.h"
#import "HAMerchantAnalysisVC.h"
#import "HAMerchantMoveVC.h"
#import "HAMerchantAddVC.h"
#import "HAWebVC.h"

@interface HAMyMerchantManageSubVC ()

/***/
@property (nonatomic, strong) UIView *mainV;
/***/
@property (nonatomic, strong) UIImageView *s1ImgV;
/***/
@property (nonatomic, strong) UILabel *s1NL;
/***/
@property (nonatomic, strong) UIImageView *s2ImgV;
/***/
@property (nonatomic, strong) UILabel *s2NL;
/***/
@property (nonatomic, strong) UIImageView *s3ImgV;
/***/
@property (nonatomic, strong) UILabel *s3NL;
/***/
@property (nonatomic, strong) UILabel *machineNumLabel;
/***/
@property (nonatomic, strong) HAMerchantManageMo *model;

@end

@implementation HAMyMerchantManageSubVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
    // Do any additional setup after loading the view.
}

- (void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"customer/items.html";
    request.parms = @{
        @"source_id":self.canalItemModel.value,
    };
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            [MBProgressHUD hideHUDForView:self.view];
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                self.model = [HAMerchantManageMo mj_objectWithKeyValues:dataModel.data];
                
                [self setupUI];

            }else {
                [MBProgressHUD showError:dataModel.msg];
            }
        }
    }];
}


-(void)itemViewClick:(UITapGestureRecognizer *)tap {
    UIView *view = tap.view;

    if (view.tag == 0) {
        HAAllMerchantVC *vc = [[HAAllMerchantVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (view.tag == 1) {
        HAMerchantMoveVC *vc = [[HAMerchantMoveVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (view.tag == 2) {
        HAMerchantAnalysisVC *vc = [[HAMerchantAnalysisVC alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (view.tag == 3) {
//        HAMerchantAddVC *vc = [[HAMerchantAddVC alloc]init];
//        [self.navigationController pushViewController:vc animated:YES];
        HAWebVC *vc = [[HAWebVC alloc]init];
        vc.tit = @"开通用户";
        vc.urlStr = [NSString stringWithFormat:@"%@index/merchant/canal.html?token=%@",kHaBaseUrl,HAUserDefaults.token];
        [self.navigationController pushViewController:vc animated:YES];

    }
}

-(void)m1BCk {
}
-(void)m2BCk {
    
}
-(void)m3BCk {
    
}

-(void)setupUI {
        
    
    UIView *topBgV = [UIView ha_createView];
    topBgV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:topBgV];
    [topBgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.height.mas_offset(175);
    }];
    
    UIView *machineNumBgView = [UIView ha_createView];
    machineNumBgView.backgroundColor = [UIColor colorWithHexString:@"#E6F1FD"];
    machineNumBgView.layer.cornerRadius = 3;
    machineNumBgView.layer.masksToBounds = YES;
    [self.view addSubview:machineNumBgView];
    [machineNumBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(20);
        make.height.mas_offset(40);
    }];
    
    UILabel *machineNumTitLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#000000" text:@"商户总数" alignment:0];
    [machineNumBgView addSubview:machineNumTitLabel];
    [machineNumTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.centerY.mas_equalTo(0);
    }];

    UILabel *machineNumLabel = [UILabel ha_BLabelWithFont:14 textColor:@"#000000" text:self.model.count alignment:2];
    self.machineNumLabel = machineNumLabel;
    [machineNumBgView addSubview:machineNumLabel];
    [machineNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(0);
    }];
    
    [topBgV addSubview:self.mainV];
    [self.mainV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_offset(72);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(-20);
    }];
    
    if (self.model.items.count > 0) {
        [self.s1ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.top.mas_equalTo(20);
            make.height.mas_offset(72);
            make.width.mas_offset((kUIScreen_w-60)/3);
        }];
    }
    if (self.model.items.count > 1) {
        [self.s2ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo(20);
            make.height.mas_offset(72);
            make.width.mas_offset((kUIScreen_w-60)/3);
        }];
    }
    if (self.model.items.count > 2) {
        [self.s3ImgV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(20);
            make.height.mas_offset(72);
            make.width.mas_offset((kUIScreen_w-60)/3);
        }];
    }


    UIView *bottomBgView = [UIView ha_createView];
    bottomBgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomBgView];
    [bottomBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(topBgV.mas_bottom).offset(10);
        make.height.mas_equalTo(180+60);
    }];
    
    UILabel *bottomTitLabel = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:@"综合功能" alignment:0];
    [bottomBgView addSubview:bottomTitLabel];
    [bottomTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.top.mas_equalTo(20);
        make.height.mas_offset(20);
    }];

    
    NSArray *titles = @[@"所有商户",@"商户转移",@"商户分析",@"新增商户"];
    NSArray *subTitles = @[@"查询我及团队的商户",@"转移直属商户致下级",@"商户营销推广分析",@"手动新增商户信息"];
    NSArray *images = @[@"ha_27",@"ha_26",@"ha_28",@"ha_18"];
    
    for (int i = 0; i < titles.count; i++) {
        NSString *title = titles[i];
        NSString *subTitle = subTitles[i];
        NSString *img = images[i];

        UIView *itemView = [self setItmeView:title subTitle:subTitle img:img];
        itemView.tag = i;
        [bottomBgView addSubview:itemView];
        CGFloat itemW = kUIScreen_w*0.5;
        CGFloat itemH = 90;
        CGFloat itemX = 0;
        CGFloat itemY = 60;
        if (i == 0) {
            itemX = 0;
            itemY = 60;
        }else if (i == 1) {
            itemX = kUIScreen_w*0.5;
            itemY = 60;
        }else if (i == 2) {
            itemX = 0;
            itemY = 90+60;
        }else if (i == 3) {
            itemX = kUIScreen_w*0.5;
            itemY = 90+60;
        }
        itemView.frame = CGRectMake(itemX, itemY, itemW, itemH);
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(itemViewClick:)];
        [itemView addGestureRecognizer:tap];
    }

}

-(UIView *)setItmeView:(NSString *)title subTitle:(NSString *)subTitle img:(NSString *)img {
    
    UIView *view = [UIView ha_createView];
    
    UIImageView *icon = [UIImageView ha_createImageView];
    icon.image = [UIImage imageNamed:img];
    [view addSubview:icon];
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.centerY.mas_equalTo(0);
        make.width.mas_offset(icon.image.size.width);
        make.height.mas_offset(icon.image.size.height);
    }];
    
    UILabel *titLabel = [UILabel ha_NLabelWithFont:16 textColor:@"#111A34" text:title alignment:0];
    [view addSubview:titLabel];
    [titLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(icon.mas_right).offset(10);
        make.centerY.mas_equalTo(-10);
        make.right.mas_equalTo(-10);
    }];

    UILabel *subTitLabel = [UILabel ha_NLabelWithFont:12 textColor:@"#888888" text:subTitle alignment:0];
    [view addSubview:subTitLabel];
    [subTitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(icon.mas_right).offset(10);
        make.centerY.mas_equalTo(10);
        make.right.mas_equalTo(-10);
    }];

    return view;
}

- (UIView *)mainV
{
    if (!_mainV) {
        _mainV = [UIView ha_createView];
    }
    return _mainV;
}

- (UIImageView *)s1ImgV
{
    if (!_s1ImgV) {
        _s1ImgV = [UIImageView ha_createImageView];
        _s1ImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(m1BCk)];
        [_s1ImgV addGestureRecognizer:tap];
        
        YHMyMerchantManageItemsModel *item = self.model.items[0];
        CGFloat imgW = (kUIScreen_w-60)/3;
        _s1ImgV.image = [UIImage gradientImageWithBounds:CGRectMake(0, 0, imgW, 120) andColors:@[[UIColor colorWithHexString:@"#4876F1"],[UIColor colorWithHexString:@"#91ABF4"]] andGradientType:0];
        [self.mainV addSubview:_s1ImgV];
        
        self.s1NL = [UILabel ha_BLabelWithFont:14 textColor:@"#ffffff" text:item.number alignment:1];
        [_s1ImgV addSubview:self.s1NL];
        [self.s1NL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
        
        UILabel *tL = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:item.label alignment:1];
        [_s1ImgV addSubview:tL];
        [tL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.s1NL.mas_bottom).offset(12);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
    }
    return _s1ImgV;
}

- (UIImageView *)s2ImgV
{
    if (!_s2ImgV) {
        _s2ImgV = [UIImageView ha_createImageView];
        _s2ImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(m2BCk)];
        [_s2ImgV addGestureRecognizer:tap];

        
        YHMyMerchantManageItemsModel *item = self.model.items[1];
        CGFloat imgW = (kUIScreen_w-60)/3;
        _s2ImgV.image = [UIImage gradientImageWithBounds:CGRectMake(0, 0, imgW, 120) andColors:@[[UIColor colorWithHexString:@"#6AE176"],[UIColor colorWithHexString:@"#93F6AF"]] andGradientType:0];
        [self.mainV addSubview:_s2ImgV];
        
        self.s2NL = [UILabel ha_BLabelWithFont:14 textColor:@"#ffffff" text:item.number alignment:1];
        [_s2ImgV addSubview:self.s2NL];
        [self.s2NL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
        
        UILabel *tL = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:item.label alignment:1];
        [_s2ImgV addSubview:tL];
        [tL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.s1NL.mas_bottom).offset(12);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
        
    }
    return _s2ImgV;
}


- (UIImageView *)s3ImgV
{
    if (!_s3ImgV) {
        _s3ImgV = [UIImageView ha_createImageView];
        _s3ImgV.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(m3BCk)];
        [_s3ImgV addGestureRecognizer:tap];

        YHMyMerchantManageItemsModel *item = self.model.items[2];
        CGFloat imgW = (kUIScreen_w-60)/3;
        _s3ImgV.image = [UIImage gradientImageWithBounds:CGRectMake(0, 0, imgW, 120) andColors:@[[UIColor colorWithHexString:@"#E95D5E"],[UIColor colorWithHexString:@"#EF9D9F"]] andGradientType:0];
        [self.mainV addSubview:_s3ImgV];
        
        self.s3NL = [UILabel ha_BLabelWithFont:14 textColor:@"#ffffff" text:item.number alignment:1];
        [_s3ImgV addSubview:self.s3NL];
        [self.s3NL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(15);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
        
        UILabel *tL = [UILabel ha_NLabelWithFont:12 textColor:@"#ffffff" text:item.label alignment:1];
        [_s3ImgV addSubview:tL];
        [tL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.s1NL.mas_bottom).offset(12);
            make.height.mas_offset(14);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];

        
    }
    return _s3ImgV;
}
@end
